package TagRate.project.pages.client;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

import com.web_bloks.widget.client.ConfigWidget;
import com.web_bloks.widget.client.GearsStatus;
import com.web_bloks.widget.client.LoginLogoutWidget;
import com.web_bloks.widget.client.StatusWidget;
import com.web_bloks.widget.client.LoginLogoutWidget.State;
import com.web_bloks.widget.client.LogoWidget;


public class Base extends Composite {
	
	VerticalPanel verticalPanel;
	FlexTable flexTableHeader;
	LoginLogoutWidget loginLogoutWidget;
	ConfigWidget configWidget;
	Label labelHeaderMessage;
	FlexTable flexTableBody;
	LogoWidget logoWidget;
	FlexTable flexTableFooter;
	GearsStatus gearsStatus;
	StatusWidget statusWidget;
	Label labelFooterMessage;
	
	public Base() {
		
		verticalPanel = new VerticalPanel();
		
		initWidget( verticalPanel );
		
		//this.add(verticalPanel, 2, 2);
		verticalPanel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
		verticalPanel.setSize("100%", "100%");

		flexTableHeader = new FlexTable();
		verticalPanel.add(flexTableHeader);
		verticalPanel.setCellVerticalAlignment(flexTableHeader, HasVerticalAlignment.ALIGN_TOP);
		verticalPanel.setCellHorizontalAlignment(flexTableHeader, HasHorizontalAlignment.ALIGN_CENTER);
		verticalPanel.setCellHeight(flexTableHeader, "40");
		verticalPanel.setCellWidth(flexTableHeader, "100%");
		flexTableHeader.setSize("100%", "40");

		loginLogoutWidget = new LoginLogoutWidget();
		flexTableHeader.setWidget(0, 3, loginLogoutWidget);
		flexTableHeader.getCellFormatter().setHorizontalAlignment(0, 3, HasHorizontalAlignment.ALIGN_CENTER);
		flexTableHeader.getCellFormatter().setWidth(0, 3, "35");
		loginLogoutWidget.setState(State.login);

		configWidget = new ConfigWidget();
		flexTableHeader.setWidget(0, 2, configWidget);
		flexTableHeader.getCellFormatter().setHorizontalAlignment(0, 2, HasHorizontalAlignment.ALIGN_CENTER);
		flexTableHeader.getCellFormatter().setWidth(0, 2, "35");

		labelHeaderMessage = new Label();
		flexTableHeader.setWidget(0, 1, labelHeaderMessage);
		labelHeaderMessage.setVisible(false);
		labelHeaderMessage.setStylePrimaryName("gwt-LabelMessage");
		flexTableHeader.getCellFormatter().setHorizontalAlignment(0, 1, HasHorizontalAlignment.ALIGN_CENTER);
		flexTableHeader.getCellFormatter().setWidth(0, 1, "100%");

		logoWidget = new LogoWidget();
		flexTableHeader.setWidget(0, 0, logoWidget);

		flexTableBody = new FlexTable();
		verticalPanel.add(flexTableBody);
		verticalPanel.setCellVerticalAlignment(flexTableBody, HasVerticalAlignment.ALIGN_TOP);
		verticalPanel.setCellHorizontalAlignment(flexTableBody, HasHorizontalAlignment.ALIGN_LEFT);
		flexTableBody.setSize("100%", "100%");
		verticalPanel.setCellHeight(flexTableBody, "100%");
		verticalPanel.setCellWidth(flexTableBody, "100%");

		flexTableFooter = new FlexTable();
		verticalPanel.add(flexTableFooter);
		flexTableFooter.setSize("100%", "40");
		verticalPanel.setCellHeight(flexTableFooter, "40");
		verticalPanel.setCellWidth(flexTableFooter, "100%");

		gearsStatus = new GearsStatus();
		flexTableFooter.setWidget(0, 0, gearsStatus);
		flexTableFooter.getCellFormatter().setHorizontalAlignment(0, 0, HasHorizontalAlignment.ALIGN_CENTER);
		flexTableFooter.getCellFormatter().setWidth(0, 0, "35");

		statusWidget = new StatusWidget();
		flexTableFooter.setWidget(0, 2, statusWidget);
		flexTableFooter.getCellFormatter().setHorizontalAlignment(0, 2, HasHorizontalAlignment.ALIGN_CENTER);
		flexTableFooter.getCellFormatter().setWidth(0, 2, "35");

		labelFooterMessage = new Label();
		flexTableFooter.setWidget(0, 1, labelFooterMessage);
		flexTableFooter.getCellFormatter().setHorizontalAlignment(0, 1, HasHorizontalAlignment.ALIGN_CENTER);
		flexTableFooter.getCellFormatter().setWidth(0, 1, "100%");
		
		setup();
	}

	public Label getLabelHeaderMessage() {
		return labelHeaderMessage;
	}
	
	public void setBody(Widget widget) {
		flexTableBody.setWidget(0, 0, widget);
	}
	
	protected void setup() {
		PageMessages pm = GWT.create(PageMessages.class);
		configWidget.setHref(pm.configpage());
		logoWidget.setHref(pm.homeurl());
	}
	
	public void setLoginLogoutUrls(String in, String out) {
		loginLogoutWidget.setLoginUrl(in);
		loginLogoutWidget.setLogoutUrl(out);
	}
	
	public void setUserState(boolean logged_in) {
		if (logged_in)
			loginLogoutWidget.setState(LoginLogoutWidget.State.logout);
		else
			loginLogoutWidget.setState(LoginLogoutWidget.State.login);
	}
}//END